/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/

#pragma once
#include "StdAfx.h"
#include <sqlite/sqlite3.h>

extern DWORD  otlkcon_tlsIndex;
extern CRITICAL_SECTION otlkcon_sqlite_cs;

// An IStream object built around a property tag stored in an sqlite row
class O_IStream : public IStream
{
    public:
    STDMETHODIMP QueryInterface( 
                 REFIID riid,
                 void **ppv);
            
    ULONG STDMETHODCALLTYPE AddRef();
            
    ULONG STDMETHODCALLTYPE Release();

    STDMETHODIMP Read( 
             void *pv,
             ULONG cb,
             ULONG *pcbRead);
        
   STDMETHODIMP Write( 
             const void *pv,
             ULONG cb,
             ULONG *pcbWritten);

    STDMETHODIMP Seek( 
            LARGE_INTEGER dlibMove,
            DWORD dwOrigin,
            ULARGE_INTEGER *plibNewPosition);

    STDMETHODIMP SetSize(ULARGE_INTEGER libNewSize);

    STDMETHODIMP CopyTo( 
            IStream *pstm,
            ULARGE_INTEGER cb,
            ULARGE_INTEGER *pcbRead,
            ULARGE_INTEGER *pcbWritten);

    STDMETHODIMP Commit(DWORD grfCommitFlags);

    STDMETHODIMP Revert();

    STDMETHODIMP LockRegion( 
            ULARGE_INTEGER libOffset,
            ULARGE_INTEGER cb,
            DWORD dwLockType);

    STDMETHODIMP UnlockRegion( 
            ULARGE_INTEGER libOffset,
            ULARGE_INTEGER cb,
            DWORD dwLockType);

    STDMETHODIMP Stat( 
            STATSTG *pstatstg,
            DWORD grfStatFlag);

    STDMETHODIMP Clone(IStream **ppstm);

    O_IStream();
    ~O_IStream();

    HRESULT Create(char * df, int pt, int fl, char *tn, int TransactionID,
                          LPALLOCATEBUFFER lb, LPALLOCATEMORE lm, LPFREEBUFFER lf);
    STDMETHODIMP_(otlkcon_sqlite_helper *) GetSqlObj();

    void Destroy();

    HRESULT TestDriver();

private:
    ULONG		cRef;
    LPBYTE		memory;
    ULONGLONG   memory_size;
    LPBYTE		memory_pointer;
    ULONGLONG   memory_bytes_remain;
    ULONGLONG  buffer_capacity;

    CRITICAL_SECTION objectCS;

    otlkcon_sqlite_helper *sqlObj;
    otlkcon_memory *memObj;

    char    *databaseFilename;
    int     propTag;
    int     flags;
    char    *tableName;
	int     nTransactionID;

    LPALLOCATEBUFFER lpAllocateBuffer;
    LPALLOCATEMORE lpAllocateMore;
    LPFREEBUFFER lpFreeBuffer;
};

